package com.yonyou.cloud.member.config;

import com.yonyou.cloud.member.exceptions.EntityIdInstantiationException;
import com.yonyou.cloud.member.domainmodel.vo.EntityId;

import java.lang.reflect.Constructor;
import java.util.function.Function;

public class EntityIdFactory {
    public static <T extends EntityId> Function<Long, T> create(Class<T> clazz) {
        try {
            Constructor<T> ctor = clazz.getConstructor(Long.class);
            return (Long id) -> {
                try {
                    return ctor.newInstance(id);
                } catch (Throwable e) {
                    throw new EntityIdInstantiationException(e);
                }
            };
        } catch (NoSuchMethodException e) {
            throw new EntityIdInstantiationException(e);
        }
    }
}